package com.study.talk.common.util;


import com.study.talk.module.entity.User;
import io.jsonwebtoken.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class JwtUtil {

    private static final long time = 24 * 60 * 60 * 1000;

    private static String jwtKey;

    private static int expirationTime;

    @Value("${jwt.signKey}")
    private String autoJwt;

    @Value("${jwt.expirationTime}")
    private int expiration;

    @PostConstruct
    public void initJwt() {
        jwtKey = this.autoJwt;
        expirationTime=this.expiration;
    }

    public static String getTokenMap(Map data, int day) {
        JwtBuilder builder = Jwts.builder();
        long expirationTime = day * time;
        String token = builder.setHeaderParam("type", "jwt")
                .setHeaderParam("alg", "HS256")
                .claim("userName", data.get("userName"))
                .claim("password", data.get("password"))
//                .claim("user", data.get("user"))
                .setExpiration(new Date(new Date().getTime() + expirationTime))
                .setId(UUID.randomUUID().toString())
                .signWith(SignatureAlgorithm.HS256, jwtKey)
                .compact();
        return token;
    }

    public static String getTokenMap(Map data) {
        return getTokenMap(data,expirationTime);
    }

    public static String getTokenUser(User user, int day) {
        JwtBuilder builder = Jwts.builder();
        long expirationTime = day * time;
        String token = builder.setHeaderParam("type", "jwt")
                .setHeaderParam("alg", "HS256")
                .claim("userName", user.getUsername())
                .claim("password", user.getPassword())
//                .claim("user", user)
                .setExpiration(new Date(new Date().getTime() + expirationTime))
                .setId(UUID.randomUUID().toString())
                .signWith(SignatureAlgorithm.HS256, jwtKey)
                .compact();
        return token;
    }
    public static String getTokenUser(User user) {
        return getTokenUser(user,expirationTime);
    }



    public static Map getBody(String token) {
        JwtParser parser = Jwts.parser();
        Jwt parse = parser.setSigningKey(jwtKey).parse(token);
        Object body = parse.getBody();
        Map map = (Map) body;
        return map;
    }

    public static Map getHeader(String token) {
        JwtParser parser = Jwts.parser();
        Jwt parse = parser.setSigningKey(jwtKey).parse(token);
        Object body = parse.getHeader();
        Map map = (Map) body;
        return map;
    }

    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userName", "userName");
        map.put("password", "userName");
        map.put("user", "userName");
        String token = getTokenMap(map);
        System.out.println(token);
        Map body = getBody(token);
        System.out.println(body);
    }
}
